package com.zhongke.cotrun.controller.manager;

import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import com.sun.istack.NotNull;
import com.zhongke.cotrun.common.annotation.SysLog;
import com.zhongke.cotrun.common.util.APIResponse;
import com.zhongke.cotrun.common.util.AddIntegralUtils;
import com.zhongke.cotrun.common.validator.Assert;
import com.zhongke.cotrun.controller.AbstractController;
import com.zhongke.cotrun.model.exception.BizException;
import com.zhongke.cotrun.model.pojo.*;
import com.zhongke.cotrun.service.ProjectInfoService;
import com.zhongke.cotrun.service.SysMessageService;
import com.zhongke.cotrun.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

/**
 * 作者 yhl
 * 日期 2020-01-05 02:02:19
 * 描述 ProjectInfo 服务
 */

@RestController("projectInfo")
@RequestMapping("/manager/projectInfo")
@CrossOrigin
@Validated
@Api(tags = "项目信息服务管理员接口")
public class ProjectInfoController extends AbstractController {
    @Autowired
    AddIntegralUtils addIntegralUtils;//用户工具积分工具类
    @Autowired
    UserInfoService userInfoService;
    @Resource
    public ProjectInfoService projectInfoService;
    @Autowired
    private SysMessageService sysMessageService;

    private static final String APPROVAL_PASS_TYPE = "PROJECT_APPROVAL_PASS"; //审核通过的字典type

    private static final String APPROVAL_REJECT_TYPE = "PROJECT_APPROVAL_REJECT"; //审核驳回的字典type

    @ApiOperation(value = "根据id查询审核通过后数据", notes = "根据id查询审核通过后数据:外网展示", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/queryById.json")
    public APIResponse<ProjectInfo> queryById(@NotNull @ApiParam(value = "对象id", required = true) Integer id) {
        Preconditions.checkNotNull(id);
        Preconditions.checkArgument(id > 0, "id(%s)必须大于0！", id);
        return APIResponse.returnSuccess(projectInfoService.selectByPrimaryKey(id));
    }

//    @ApiOperation(value = "根据id查询未审核数据", notes = "根据id查询未审核数据:审核详情页使用", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
//    @RequestMapping(value = "/queryAuditDetailById.json")
//    @ResponseBody
////    @RequiresPermissions("sys:project:info")
//    public APIResponse<ProjectInfo> queryAuditDetailById(@NotNull @ApiParam(value = "对象id", required = true) Integer id) {
//        Preconditions.checkNotNull(id);
//        Preconditions.checkArgument(id > 0, "id(%s)必须大于0！", id);
//        return APIResponse.returnSuccess(projectInfoService.selectAuditDetailByPrimaryKey(id));
//    }

    @ApiOperation(value = "保存或更新未审核的项目数据", notes = "保存或更新未审核的项目数据，进审核表，保存或修改项目信息时使用", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/saveOrUpdateHistory.json")
    @ResponseBody
    @RequiresPermissions("sys:project:saveOrUpdate")
    @SysLog(value = "新增或修改项目", type = "项目技术")
    public APIResponse<Boolean> saveOrUpdateHistory(@ApiParam(value = "未审核的项目数据", required = true) @Validated ProjectInfo record) throws BizException {
        Preconditions.checkNotNull(record);
        return APIResponse.returnSuccess(projectInfoService.insertSelectiveForHistory(record) > 0);
    }

    @ApiOperation(value = "分页查询项目列表", notes = "分页查询项目列表", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "/pageQueryList.json", method = RequestMethod.GET)
    @ResponseBody
    public APIResponse pageQueryList(@ApiParam(value = "分页请求参数", required = true) @Validated ProjectInfoVo param) {
        Preconditions.checkNotNull(param);
        Preconditions.checkArgument(param.getPageIndex() > 0 && param.getPageSize() > 0);
        param.checkQueryParam();
        int count = projectInfoService.queryUnApprovalCounts();
        PageInfo info = projectInfoService.pageQueryProjectListForManager(param);
        Map data = new HashMap(4);
        data.put("count", count);
        data.put("data", info);
        return APIResponse.returnSuccess(data);
    }

    /**
     * 根据id删除对象
     */
    @ResponseBody
    @ApiOperation(value = "删除", notes = "删除", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/deleteById.json")
    @RequiresPermissions("sys:project:delete")
    @SysLog(value = "删除项目", type = "项目技术")
    public APIResponse deleteById(@ApiParam(value = "对象id", required = true) Integer id) {
        Preconditions.checkNotNull(id);
        Preconditions.checkArgument(id > 0, "id(%s)必须大于0！", id);
        if (projectInfoService.deleteByPrimaryKey(id) > 0) {
            return APIResponse.returnSuccess("操作成功");
        } else {
            return APIResponse.returnFail("操作失败");
        }
    }

    /**
     * 批量删除
     *
     * @param ids 主键ids(多个id用英文逗号分隔)
     * @return APIResponse
     */
    @ApiOperation(value = "批量删除", notes = "批量删除", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequiresPermissions("sys:project:delete")
    @SysLog(value = "批量删除", type = "项目技术")
    @PostMapping("/deleteByBatch.json")
    public APIResponse deleteByBatch(@ApiParam(value = "对象id,多个id用英文逗号分隔", required = true) String ids) {
        Assert.isNull(ids, "主键ID不能为空");
        if (projectInfoService.deleteBatch(ids)) {
            return APIResponse.returnSuccess("操作成功");
        } else {
            return APIResponse.returnFail("操作失败");
        }
    }

    /**
     * 根据id下线项目
     */
    @ResponseBody
    @ApiOperation(value = "下线", notes = "下线", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/cancelById.json")
    @RequiresPermissions("sys:project:cancel")
    @SysLog(value = "下线项目", type = "项目技术")
    public APIResponse cancelById(@ApiParam(value = "对象id", required = true) Integer id) {
        Preconditions.checkNotNull(id);
        Preconditions.checkArgument(id > 0, "id(%s)必须大于0！", id);
        if (projectInfoService.cancelByPrimaryKey(id)) {
            return APIResponse.returnSuccess("操作成功");
        } else {
            return APIResponse.returnFail("操作失败");
        }
    }

    /**
     * 根据id通过项目
     */
    @ResponseBody
    @ApiOperation(value = "审批通过", notes = "审批通过", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/approvalPass.json")
    @RequiresPermissions("sys:project:approval")
    @SysLog(value = "审核通过", type = "项目技术")
    public APIResponse approvalPass(@ApiParam(value = "未审核的项目数据", required = true) @Validated ProjectInfo record, HttpSession session) throws ParseException {
        Preconditions.checkNotNull(record);
        if (projectInfoService.approvalPass(record)) {
            SysMessageEntity messageEntity= sysMessageService.saveSysMessage(APPROVAL_PASS_TYPE,record.getProjectId().longValue(),"project");
            //获取用户信息
            UserInfo userInfo1 = userInfoService.selectUserInfoByid(record.getCreateId().intValue());
            Map m = addIntegralUtils.getMapOfUserEntity(userInfo1, 0, "/manager/projectInfo/approvalPass.json",
                    "VIP0", "用户发布项目的时候增加:", session);
            return APIResponse.returnSuccess("操作成功");
        } else {
            return APIResponse.returnFail("操作失败");
        }
    }

    /**
     * 根据id驳回项目
     */
    @ResponseBody
    @ApiOperation(value = "审批驳回", notes = "审批驳回", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/approvalRejected.json")
    @RequiresPermissions("sys:project:approval")
    @SysLog(value = "审核驳回", type = "项目技术")
    public APIResponse approvalRejected(@ApiParam(value = "对象id", required = true) Integer id, @ApiParam(value = "驳回原因说明", required = true) String opinion) {
        Preconditions.checkNotNull(id);
        Map<String, Object> map = Maps.newHashMapWithExpectedSize(7);
        Preconditions.checkArgument(id > 0, "id(%s)必须大于0！", id);
        Assert.isNull(opinion, "驳回原因不能为空");
        Integer state = 2;
        if (projectInfoService.approvalByPrimaryKey(id, state, opinion)) {
            SysMessageEntity messageEntity= sysMessageService.saveSysMessage(APPROVAL_REJECT_TYPE,id.longValue(),"project");
            return APIResponse.returnSuccess("操作成功");
        } else {
            return APIResponse.returnFail("操作失败");
        }
    }

    /**
     * 设置推荐
     */
    @ResponseBody
    @ApiOperation(value = "批量设置推荐", notes = "批量设置推荐", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/recommendProject.json")
    @RequiresPermissions("sys:project:approval")
    @SysLog(value = "设置推荐", type = "项目技术")
    public APIResponse recommendProject(@ApiParam(value = "对象id,多个id用英文逗号分隔", required = true) String ids) {
        Assert.isNull(ids, "参数不能为空");
        if (projectInfoService.recommendProject(ids)) {
            return APIResponse.returnSuccess("操作成功");
        } else {
            return APIResponse.returnFail("操作失败");
        }
    }

    /**
     * 设置推荐
     */
    @ResponseBody
    @ApiOperation(value = "设置推荐", notes = "设置推荐", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/setRecommend.json")
    @RequiresPermissions("sys:project:approval")
    @SysLog(value = "设置推荐", type = "项目技术")
    public APIResponse setRecommend(@ApiParam(value = "对象id", required = true) Integer id, @ApiParam(value = "推荐状态 0:否 1:是", required = true) Integer state) {
        Assert.isNull(id, "参数不能为空");
        Assert.isNull(state, "参数不能为空");
        if (projectInfoService.setRecommend(id, state)) {
            return APIResponse.returnSuccess("操作成功");
        } else {
            return APIResponse.returnFail("操作失败");
        }
    }

}
